package com.project.controller;

import com.project.pojo.InsuranceTravelTemp;
import com.project.pojo.Order;
import com.project.pojo.Ordercopy;
import com.project.service.InsuranceTravelService;
import com.project.service.OrderService;
import com.project.utils.ResponseData;
import com.project.utils.ResultEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/travel")
public class TravelOrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    InsuranceTravelService insuranceTravelService;

    /**
     *
     * @param tempDTO 前端传回用户填写的保险信息
     * @param userId  用户id
     * @param insuranceType 保险类型
     * @return
     */
    @RequestMapping("/insert")
    @Transactional
    public ResponseData insertOrder(@RequestBody InsuranceTravelTemp tempDTO, @RequestParam int userId, @RequestParam int insuranceType){
        Order order = new Order(userId, insuranceType);
        order.setStartTime(new Date(System.currentTimeMillis()));
        insertOrder(order);
        int orderId =order.getId();
        Ordercopy ordercopy = new Ordercopy(order.getUserId(), order.getInsurType(), order.getPassOne(), order.getOneSuggest(), order.getPassTwo(), order.getTwoSuggest(), order.getStatus(),order.getStartTime() , order.getCheck(), orderId, order.getDistributedstate());
        if (orderService.insertOrderCopy(ordercopy)>0){
            tempDTO.setAssociatedorder(orderId);
            if (insuranceTravelService.insertTravelTemp(tempDTO)>0){
                return new ResponseData(ResultEnums.CREATED);
            }
        }
        return  new ResponseData(ResultEnums.NOT_FOUND);
    }

    public void insertOrder(Order order ){
        orderService.insertOrder(order);
    }
}
